#pragma once

#include "yavn3D.h"

namespace Uravnenie3D {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>

	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  CalcButton;
	private: System::Windows::Forms::DataVisualization::Charting::Chart^  chart1;
	private: System::Windows::Forms::Button^  PresentationUXTButton;
	private: System::Windows::Forms::Button^  PresentationUYTButton;
	private: System::Windows::Forms::Button^  PresentationUZTButton;
	private: System::Windows::Forms::Label^  TimeLabel;
	private: System::Windows::Forms::Button^  PlusButton;
	private: System::Windows::Forms::Button^  MinusButton;
	private: System::Windows::Forms::Label^  TimeInfoLabel;


	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::Windows::Forms::DataVisualization::Charting::ChartArea^  chartArea1 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
			System::Windows::Forms::DataVisualization::Charting::Legend^  legend1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Legend());
			System::Windows::Forms::DataVisualization::Charting::Series^  series1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			this->CalcButton = (gcnew System::Windows::Forms::Button());
			this->chart1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
			this->PresentationUXTButton = (gcnew System::Windows::Forms::Button());
			this->PresentationUYTButton = (gcnew System::Windows::Forms::Button());
			this->PresentationUZTButton = (gcnew System::Windows::Forms::Button());
			this->TimeLabel = (gcnew System::Windows::Forms::Label());
			this->PlusButton = (gcnew System::Windows::Forms::Button());
			this->MinusButton = (gcnew System::Windows::Forms::Button());
			this->TimeInfoLabel = (gcnew System::Windows::Forms::Label());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->chart1))->BeginInit();
			this->SuspendLayout();
			// 
			// CalcButton
			// 
			this->CalcButton->Location = System::Drawing::Point(28, 39);
			this->CalcButton->Name = L"CalcButton";
			this->CalcButton->Size = System::Drawing::Size(141, 23);
			this->CalcButton->TabIndex = 0;
			this->CalcButton->Text = L"Calculate";
			this->CalcButton->UseVisualStyleBackColor = true;
			this->CalcButton->Click += gcnew System::EventHandler(this, &Form1::CalcButton_Click);
			// 
			// chart1
			// 
			chartArea1->Name = L"ChartArea1";
			this->chart1->ChartAreas->Add(chartArea1);
			legend1->Name = L"Legend1";
			this->chart1->Legends->Add(legend1);
			this->chart1->Location = System::Drawing::Point(200, 12);
			this->chart1->Name = L"chart1";
			series1->ChartArea = L"ChartArea1";
			series1->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::Line;
			series1->Legend = L"Legend1";
			series1->Name = L"Series1";
			this->chart1->Series->Add(series1);
			this->chart1->Size = System::Drawing::Size(465, 260);
			this->chart1->TabIndex = 1;
			this->chart1->Text = L"chart1";
			// 
			// PresentationUXTButton
			// 
			this->PresentationUXTButton->Location = System::Drawing::Point(28, 129);
			this->PresentationUXTButton->Name = L"PresentationUXTButton";
			this->PresentationUXTButton->Size = System::Drawing::Size(141, 23);
			this->PresentationUXTButton->TabIndex = 2;
			this->PresentationUXTButton->Text = L"presentation U(x,t)";
			this->PresentationUXTButton->UseVisualStyleBackColor = true;
			this->PresentationUXTButton->Click += gcnew System::EventHandler(this, &Form1::PresentationUXTButton_Click);
			// 
			// PresentationUYTButton
			// 
			this->PresentationUYTButton->Location = System::Drawing::Point(28, 171);
			this->PresentationUYTButton->Name = L"PresentationUYTButton";
			this->PresentationUYTButton->Size = System::Drawing::Size(141, 23);
			this->PresentationUYTButton->TabIndex = 3;
			this->PresentationUYTButton->Text = L"presentation U(y,t)";
			this->PresentationUYTButton->UseVisualStyleBackColor = true;
			this->PresentationUYTButton->Click += gcnew System::EventHandler(this, &Form1::PresentationUYTButton_Click);
			// 
			// PresentationUZTButton
			// 
			this->PresentationUZTButton->Location = System::Drawing::Point(28, 213);
			this->PresentationUZTButton->Name = L"PresentationUZTButton";
			this->PresentationUZTButton->Size = System::Drawing::Size(141, 23);
			this->PresentationUZTButton->TabIndex = 4;
			this->PresentationUZTButton->Text = L"presentation U(z,t)";
			this->PresentationUZTButton->UseVisualStyleBackColor = true;
			this->PresentationUZTButton->Click += gcnew System::EventHandler(this, &Form1::PresentationUZTButton_Click);
			// 
			// TimeLabel
			// 
			this->TimeLabel->AutoSize = true;
			this->TimeLabel->Location = System::Drawing::Point(49, 77);
			this->TimeLabel->Name = L"TimeLabel";
			this->TimeLabel->Size = System::Drawing::Size(0, 13);
			this->TimeLabel->TabIndex = 5;
			// 
			// PlusButton
			// 
			this->PlusButton->Location = System::Drawing::Point(138, 93);
			this->PlusButton->Name = L"PlusButton";
			this->PlusButton->Size = System::Drawing::Size(31, 30);
			this->PlusButton->TabIndex = 6;
			this->PlusButton->Text = L"+";
			this->PlusButton->UseVisualStyleBackColor = true;
			this->PlusButton->Click += gcnew System::EventHandler(this, &Form1::PlusButton_Click);
			// 
			// MinusButton
			// 
			this->MinusButton->Location = System::Drawing::Point(28, 93);
			this->MinusButton->Name = L"MinusButton";
			this->MinusButton->Size = System::Drawing::Size(31, 30);
			this->MinusButton->TabIndex = 7;
			this->MinusButton->Text = L"-";
			this->MinusButton->UseVisualStyleBackColor = true;
			this->MinusButton->Click += gcnew System::EventHandler(this, &Form1::MinusButton_Click);
			// 
			// TimeInfoLabel
			// 
			this->TimeInfoLabel->AutoSize = true;
			this->TimeInfoLabel->Location = System::Drawing::Point(82, 102);
			this->TimeInfoLabel->Name = L"TimeInfoLabel";
			this->TimeInfoLabel->Size = System::Drawing::Size(13, 13);
			this->TimeInfoLabel->TabIndex = 8;
			this->TimeInfoLabel->Text = L"0";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(690, 299);
			this->Controls->Add(this->TimeInfoLabel);
			this->Controls->Add(this->MinusButton);
			this->Controls->Add(this->PlusButton);
			this->Controls->Add(this->TimeLabel);
			this->Controls->Add(this->PresentationUZTButton);
			this->Controls->Add(this->PresentationUYTButton);
			this->Controls->Add(this->PresentationUXTButton);
			this->Controls->Add(this->chart1);
			this->Controls->Add(this->CalcButton);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->chart1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		int currtime;
		double** data;
		double deltaT;
		double deltaP;
		int pointCount;
		int maxTime;

		private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
		pointCount = -1;
		maxTime = 0;

		 }

		void clamping()
		{
			int maxTime = getMaxTime();
			int maxCoord = getMaxCoord();

			data = getUX();
			for (int t=0; t<maxTime; t++)
				for (int x=0; x<maxCoord; x++)
				{
					if (data[t][x] < 1e-10) data[t][x] = 1e-10;
					if (data[t][x] > 1e10) data[t][x] = 1e10;
				};

			data = getUY();
			for (int t=0; t<maxTime; t++)
				for (int x=0; x<maxCoord; x++)
				{
					if (data[t][x] < 1e-10) data[t][x] = 1e-10;
					if (data[t][x] > 1e10) data[t][x] = 1e10;
				};

			data = getUZ();
			for (int t=0; t<maxTime; t++)
				for (int x=0; x<maxCoord; x++)
				{
					if (data[t][x] < 1e-10) data[t][x] = 1e-10;
					if (data[t][x] > 1e10) data[t][x] = 1e10;
				};
		}

	private: System::Void CalcButton_Click(System::Object^  sender, System::EventArgs^  e) {
				 currtime = 0;

				 int count = 9;
				 char* callString[] = {"very_cool_software", "10", "10", "10",
					 "99", "99", "99", 
					 "0.1", "99"};
				 run(count, callString);

				 clamping();

				 TimeLabel->Text = "Time: "+System::Convert::ToString(GetTime());

				 maxTime = getMaxTime();
				 deltaT = (0.1 - 0.0)/maxTime;
			 }

private: System::Void PresentationUXTButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 data = getUX();
			 pointCount = getMaxCoord();
			 deltaP = (10.0 - 0.0) / pointCount;
		 }
private: System::Void PresentationUYTButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 data = getUY();
			 pointCount = getMaxCoord();
			deltaP = (10.0 - 0.0) / pointCount;
		 }

private: System::Void PresentationUZTButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 data = getUZ();
			 pointCount = getMaxCoord();
			 deltaP = (10.0 - 0.0) / pointCount;
		 }

private: System::Void MinusButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 currtime--;

			 TimeInfoLabel->Text = Convert::ToString(0.0+currtime*deltaT);

			 if (currtime >= 0 && currtime < maxTime)
			 {
				 chart1->Series[0]->Points->Clear();
				 for (int i=0; i<pointCount; i++)
					 chart1->Series[0]->Points->AddXY(0.0 + i*deltaP, data[currtime][i]);
			 }
		 }
private: System::Void PlusButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 currtime++;

			 TimeInfoLabel->Text = Convert::ToString(0.0+currtime*deltaT);

			 if (currtime >= 0 && currtime < maxTime)
			 {
				 chart1->Series[0]->Points->Clear();
				 for (int i=0; i<pointCount; i++)
					 chart1->Series[0]->Points->AddXY(0.0 + i*deltaP, data[currtime][i]);
			 }
		 }

};
}

